﻿using CorpStore.Common;
using CorpStore.Models;
using CorpStore.ViewModels;
using Newtonsoft.Json;
using StoreHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace CorpStore
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class AppDetailsPage : BasePage
    {
        AppDetailsViewModel viewModel = new AppDetailsViewModel();

        public AppDetailsPage()
        {
            InitializeComponent();
            DataContext = viewModel;
        }

        protected async override void LoadState(object navigationParameter, Dictionary<string, object> pageState)
        {
            await viewModel.InitOrRestoreStateAsync(navigationParameter as string, pageState);

            // init some UI
            backButton.Visibility = CanGoBackVisibility;

            if (viewModel.CurrentApp.Screenshots.Count > 0)
                viewModel.SelectedScreenshot = viewModel.CurrentApp.Screenshots[0];
        }


        // save our currently selected app in our page state before leaving
        protected override void SaveState(Dictionary<string, object> pageState)
        {
            viewModel.SaveState(ref pageState);
        }


        private async void btnInstallRun_Click(object sender, RoutedEventArgs e)
        {
            await viewModel.LaunchAppAsync((PackageInfo)viewModel.CurrentApp);
        }



        private void lstScreenshots_ItemClick(object sender, ItemClickEventArgs e)
        {
            viewModel.SelectedScreenshot = e.ClickedItem as ScreenshotInfo;

            if (viewModel.SelectedScreenshot != null)
            {
                imgLargeScreenshot.Source = new BitmapImage(new Uri(viewModel.SelectedScreenshot.Filename));
            }

        }


        private async void btnPinUnpin_Click(object sender, RoutedEventArgs e)
        {
            await viewModel.PinUnpinCurrentAppAsync();
        }


    }
}
